extends Node2D

@export var map_scene: PackedScene
var map:Map

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	map = RioladoMap.load_map()
	map.position = Vector2i.ZERO
	add_child(map)
	$player.position = GridUtils.grid_to_world(map.default_entered_location)
	
	_on_viewport_resized()
	get_viewport().connect("size_changed", _on_viewport_resized)
	
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass

# 
func _on_viewport_resized() -> void:
	var viewport_size = get_viewport_rect().size
	if viewport_size.x/$player/Camera2D.zoom.x > map.size.x * GridUtils.CELL_SIZE :
		print_debug("地图尺寸",map.size.x * GridUtils.CELL_SIZE,"小于视窗尺寸，相机x轴限制在视窗内：",viewport_size.x/$player/Camera2D.zoom.x)
		$player/Camera2D.limit_right=  viewport_size.x/$player/Camera2D.zoom.x 
	else:
		$player/Camera2D.limit_right=map.size.x * GridUtils.CELL_SIZE
	if viewport_size.y/$player/Camera2D.zoom.y > map.size.y * GridUtils.CELL_SIZE :
		print_debug("地图尺寸",map.size.y * GridUtils.CELL_SIZE,"小于视窗尺寸相机y轴限制在视窗内：",viewport_size.y/$player/Camera2D.zoom.y)
		$player/Camera2D.limit_bottom=  viewport_size.y/$player/Camera2D.zoom.y
	else:
		$player/Camera2D.limit_bottom=map.size.y * GridUtils.CELL_SIZE
	$player/Camera2D.limit_left=0
	$player/Camera2D.limit_top=0
